import 'package:package_info_plus/package_info_plus.dart';

/**
 * @Description: 包名信息工具类
 * @Author: 下雨了
 * @CreateDate: 2021-12-15 15:59
 * @UpdateUser: 更新者
 * @UpdateDate:  2021-12-15 15:59
 * @UpdateRemark: 更新说明
 * @Version: 1.0
 */
///
//定义一个top-level（全局）变量，页面引入该文件后可以直接使用 PackageInfoUtils
// final packageInfo = PackageInfoUtils();

class PackageInfoUtils {
  // 私有构造函数
  PackageInfoUtils._internal();

  // 保存单例
  static final PackageInfoUtils _packageInfoUtils = PackageInfoUtils._internal();

  // 工厂构造函数
  factory PackageInfoUtils() => _packageInfoUtils;

  Future<PackageInfo> _initPackageInfo() async {
    PackageInfo info = await PackageInfo.fromPlatform();
    return info;
  }

  Future<String> getAppName() async {
    return await _initPackageInfo().then((value) => value.appName);
  }

  Future<String> getPackageName() async {
    return await _initPackageInfo().then((value) => value.packageName);
  }

  Future<String> getVersion() async {
    PackageInfo info = await PackageInfo.fromPlatform();
    return info.version;
  }

  Future<String> getBuildNumber() async {
    return await _initPackageInfo().then((value) => value.buildNumber);
  }
}
